package cn.itsource.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

//Token转发
@Slf4j
public class TokenInterceptor implements RequestInterceptor {

    //读取配置中的Token名字
    @Value("${sa-token.token-name}")
    private String tokenName;

    @Override
    public void apply(RequestTemplate requestTemplate) {

        //请求上下文 - 获取属性
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String tokenValue = requestAttributes.getRequest().getHeader(tokenName);
        log.info("Fein 转发Token {}",tokenValue);
        requestTemplate.header(tokenName,tokenValue);
    }
}
